package com.future.mayi.fragment;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;

import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.activity.BaseFragment;
import com.future.baselib.utils.CommonUtils;
import com.future.mayi.R;
import com.future.mayi.activity.ActivitiesActivity;
import com.future.mayi.activity.GoodsActivity;
import com.future.mayi.activity.MainActivity;
import com.future.mayi.activity.SearchActivity;
import com.future.mayi.activity.WebActivity;
import com.future.mayi.adapter.IndexGoodsAdapter;
import com.future.mayi.adapter.IndexRecommendAdapter;
import com.future.mayi.adapter.IndexRecommendAdapter2;
import com.future.mayi.app.App;
import com.future.mayi.model.BannerModel;
import com.future.mayi.model.IndexIconModel;
import com.future.mayi.model.IndexModel;
import com.future.mayi.model.interfaces.OnItemClickListener;
import com.future.mayi.response.IndexResponse;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.view.xrecyclerview.XRecyclerView;
import com.future.mayi.widget.CarouselTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class IndexFragment extends BaseFragment implements OnItemClickListener, XRecyclerView.LoadingListener, CarouselTitleView.OnItemPageClick {


    @BindView(R.id.recycler_view)
    XRecyclerView recyclerView;
    //    @BindView(R.id.iv_message)
//    ImageView ivMessage;
//    @BindView(R.id.tv_message_count)
//    TextView tvMessageCount;
//    @BindView(R.id.rl_title_bar)
    RelativeLayout rlTitleBar;

    private CarouselTitleView carouselView;
    //    private GridView gridView;
    private RecyclerView rvHomeIcon;

    private IndexGoodsAdapter adapter;
    private IndexRecommendAdapter2 recommendAdapter;


    private int height = 45;

    private IndexModel indexModel;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_index;
    }


    @Override
    protected void initView() {
        height = CommonUtils.dip2px(getContext(), height);

        View view = View.inflate(getContext(), R.layout.layout_index_head, null);
        rlTitleBar = view.findViewById(R.id.rl_title_bar);
        view.findViewById(R.id.fl_main_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mContext, SearchActivity.class));
            }
        });
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        recyclerView.addHeaderView(view);
        carouselView = view.findViewById(R.id.carousel_view);
//        gridView = view.findViewById(R.id.grid_view);
        rvHomeIcon = view.findViewById(R.id.rv_home_icon);
        rvHomeIcon.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        recommendAdapter = new IndexRecommendAdapter2(getActivity(), new ArrayList<IndexIconModel>());
        rvHomeIcon.setAdapter(recommendAdapter);
        adapter = new IndexGoodsAdapter(getActivity(), recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLoadingMoreEnabled(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);
        if (MainActivity.statusBar) {
            ViewGroup.LayoutParams layoutParams = rlTitleBar.getLayoutParams();
            height = MainActivity.statusBarHeight + height;
            layoutParams.height = height;
            rlTitleBar.setPadding(0, MainActivity.statusBarHeight, 0, 0);
        }
    }

    @Override
    protected void initListener() {
        recyclerView.setLoadingListener(this);
//        adapter.setOnItemClickListener(this);
//        gridView.setOnItemClickListener(this);
        recommendAdapter.setOnItemClickListener(this);
//        rvHomeIcon.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        recyclerView.refresh();
    }


    public void showFragment() {

    }

    public void refresh() {

    }

    public void refreshMsgCount(int count) {
//        if (tvMessageCount != null) {
//            if (count > 0) {
//                tvMessageCount.setVisibility(View.VISIBLE);
//                tvMessageCount.setText(count > 99 ? "99+" : String.valueOf(count));
//            } else tvMessageCount.setVisibility(View.GONE);
//        }
    }


    @OnClick({
//            R.id.fl_main_search,
//            R.id.iv_message,
            R.id.iv_top})
    public void onViewClicked(View view) {
        switch (view.getId()) {
//            case R.id.iv_message:
//                if (ClickUtils.isNoLogin(getActivity(), true)) return;
//                startActivity(new Intent(getContext(), SystemMessageActivity.class));
//                break;

//            case R.id.fl_main_search:
//                startActivity(new Intent(mContext, SearchActivity.class));
//                break;
            case R.id.iv_top:
                recyclerView.refresh();
                break;
        }
    }

    @Override
    public void OnItem(View view, int position) {
        onItemClick(position);
    }

    private void onItemClick(int position) {
        IndexIconModel model = recommendAdapter.getItem(position);
        //跳转方式:0=不跳转,1=单品详情,2=活动会场,10=url
        if (model.getJump_type() == 1) {
            GoodsActivity.startActivity(getActivity(), Integer.parseInt(model.getRelation_id()));
        } else if (model.getJump_type() == 2) {
            ActivitiesActivity.startActivity(getActivity(), Integer.parseInt(model.getRelation_id()));
        } else if (model.getJump_type() == 10) {
            WebActivity.startActivity(getActivity(), model.getRelation_id(), null);
        }
    }

    @Override
    public void onRefresh() {
        getData();
    }

    @Override
    public void onLoadMore() {

    }


    private void getData() {
        new HttpRequest<IndexResponse>()
                .with(getActivity()).setApiCode(ApiCst.getIndex)
                .addParam("token", App.getToken(mContext))
                .setListener(new HttpRequest.OnNetworkListener<IndexResponse>() {
                    @Override
                    public void onSuccess(IndexResponse response) {
                        indexModel = response.getModel();
                        setView();
                        HCUtils.refreshListForPage(recyclerView, adapter, indexModel.getGoods(), 0, 10);
                    }

                    @Override
                    public void onFail(String message, IndexResponse response) {
                        HCUtils.refreshFail(recyclerView, 0, getContext(), message);
                    }
                }).start(new IndexResponse());


    }

//    private int itemHeight;
//    private int itemWidth;

    private void setView() {

//        int size = 0;
//        if (gridView.getLayoutParams() != null && indexModel.getIcon() != null && indexModel.getIcon().size() > 0) {
//            itemWidth = (int) (((float) (CommonUtils.getScreenSize(getActivity())[0] - CommonUtils.dip2px(getActivity(), 12))) / 2.0f);
//            itemHeight = (int) ((itemWidth - CommonUtils.dip2px(getActivity(), 12)) * (90.0f / 170.0f));
//            size = indexModel.getIcon().size();
//            gridView.setNumColumns(size);
//            gridView.getLayoutParams().height = itemHeight;
//            gridView.getLayoutParams().width = itemWidth * size;
//        }

//        if (recommendAdapter == null) {
////            recommendAdapter = new IndexRecommendAdapter(getActivity(), indexModel.getIcon(), itemHeight);
////            gridView.setAdapter(recommendAdapter);
//            recommendAdapter = new IndexRecommendAdapter2(getActivity(), indexModel.getIcon());
//            rvHomeIcon.setAdapter(recommendAdapter);
//        } else {
        recommendAdapter.setList(indexModel.getIcon());
//        }


        carouselView.setBannerModel(indexModel.getBanner());
        carouselView.setOnItemPageClick(this);

    }


    @Override
    public void OnLongItem(View view, int position) {

    }


    //广告点击
    @Override
    public void onItemPage(int position) {
        //banner跳转方式 0-不跳转 1-单品详情页 2-活动会场 3-url
        if (indexModel == null) return;
        if (indexModel.getBanner() == null) return;
        if (indexModel.getBanner().size() < position + 1) return;
        BannerModel bannerModel = indexModel.getBanner().get(position);
        if (bannerModel == null) return;
        if (bannerModel.getJump_type() == 0) return;
        //0=不跳转,1=商品详情,2=活动会场,10=url
        if (bannerModel.getJump_type() == 1) {
            GoodsActivity.startActivity(getActivity(), Integer.parseInt(bannerModel.getRelation_id()));
        } else if (bannerModel.getJump_type() == 2) {
            ActivitiesActivity.startActivity(getActivity(), Integer.parseInt(bannerModel.getRelation_id()));
        } else if (bannerModel.getJump_type() == 10) {
            WebActivity.startActivity(getActivity(), bannerModel.getRelation_id(), null);
        }

    }


//    @Override
//    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//        IndexIconModel model = (IndexIconModel) parent.getAdapter().getItem(position);
//        //跳转方式:0=不跳转,1=单品详情,2=活动会场,10=url
//        if (model.getJump_type() == 1) {
//            GoodsActivity.startActivity(getActivity(), Integer.parseInt(model.getRelation_id()));
//        } else if (model.getJump_type() == 2) {
//            ActivitiesActivity.startActivity(getActivity(), Integer.parseInt(model.getRelation_id()));
//        } else if (model.getJump_type() == 10) {
//            WebActivity.startActivity(getActivity(), model.getRelation_id(), null);
//        }
//    }
}
